
package com.eltonkent.ui;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eltonkent.R;
import com.eltonkent.db.SafeEntryTable;
import com.eltonkent.ui.containers.TemplateFieldContainer;
import com.eltonkent.util.ImageUtil;

public class ViewSafeEntryActivity extends ParentActivity
{
	private LinearLayout contentView;
	private String folderName;
	private String templateName;
	private String templateUID;
	private String iconId;
	private static final int ACTIVITY_EDIT_TEMPLATE = 20;
	private ArrayList<TemplateFieldContainer> entryFieldList;

	private void addEntryField(TemplateFieldContainer fieldDescriptor, LayoutInflater inflater)
	{
		View view = inflater.inflate(R.layout.panel_templatefield, null);
		TextView title = (TextView) view.findViewById(R.id.templateTitle);
		TextView text = (TextView) view.findViewById(R.id.templateField);
		title.setText(fieldDescriptor.getFieldName());
		text.setText(fieldDescriptor.getFieldValue());
		int textSize = getApplicationPreferences().getInt(ApplicationConstants.PREF_TEXT_SIZE,
				ApplicationConstants.PREF_DEF_TEXTSIZE);
		title.setTextSize(textSize);
		text.setTextSize(textSize);
		contentView.addView(view);

	}

	private void closeActivity(int code)
	{
		setResult(code);
		removeTextOnlyFromPath(templateName);
		finish();
	}

	private Dialog getDeleteDialog()
	{
		final Dialog dialog = getCustomDialog(R.layout.dialog_deletefolder, R.string.delete_entry);
		TextView tv = (TextView) dialog.findViewById(R.id.dialogText);
		tv.setText(getResources().getString(R.string.delete) + " " + templateName + "?");
		Button[] okCancel = getOKCancelButtons(dialog);
		Button btnok = okCancel[0];
		Button btnCancel = okCancel[1];
		btnCancel.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				dialog.dismiss();
			}
		});
		btnok.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				SafeEntryTable table = new SafeEntryTable(getApplicationContext());
				table.deleteEntry(templateUID, folderName);
				dialog.dismiss();
				closeActivity(RESULT_OK);
			}
		});
		return dialog;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode != RESULT_CANCELED)
		{
			switch(requestCode)
			{
				case ACTIVITY_EDIT_TEMPLATE:
					reloadContents();
					break;

				default:
					break;
			}
		}
	}

	@Override
	protected final Dialog onCreateDialog(int id)
	{
		switch(id)
		{
			case ApplicationConstants.DIALOG_DELETE:
				return getDeleteDialog();
			default:
				return super.onCreateDialog(id);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		inflateMenu(menu, R.menu.viewtemplatemenu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			Log.d("UI", "Back key pressed on folder contents");
			closeActivity(RESULT_CANCELED);
			return true;
		}
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
			case R.id.mnu_DeleteEntry:
				showDialog(ApplicationConstants.DIALOG_DELETE);
				return true;
			case R.id.mnu_Edit:
				Intent intent = new Intent(getApplicationContext(), EditTemplateActivity.class);
				intent.putParcelableArrayListExtra(ApplicationConstants.EXTRA_FIELD_CONTAINER,
						entryFieldList);
				intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE_ICON, iconId);
				intent.putExtra(ApplicationConstants.EXTRA_FOLDER_NAME, folderName);
				intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE_NAME, templateName);
				intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE_ISNEW, false);
				intent.putExtra(ApplicationConstants.EXTRA_TEMPLATE_UID, templateUID);
				startActivityForResult(intent, ACTIVITY_EDIT_TEMPLATE);
				return true;

		}
		return false;

	}

	@SuppressWarnings("unchecked")
	@Override
	protected void populateUI()
	{
		Intent intent = getIntent();
		/* set the entry image */
		ImageView templateImage = (ImageView) findViewById(R.id.templateIcon);
		iconId = intent.getStringExtra(ApplicationConstants.EXTRA_TEMPLATE_ICON);
		Bitmap bitmap = ImageUtil.createBitmapFromAsset(getApplicationContext(), iconId);
		templateImage.setImageBitmap(bitmap);
		/* set the entry title */
		TextView templateTitle = (TextView) findViewById(R.id.templateName);
		templateName = intent.getStringExtra(ApplicationConstants.EXTRA_TEMPLATE_NAME);
		templateTitle.setText(templateName);
		addToPath(templateName);
		/* get the current folder name */
		folderName = intent.getStringExtra(ApplicationConstants.EXTRA_FOLDER_NAME);
		/* get the entry UID */
		templateUID = intent.getStringExtra(ApplicationConstants.EXTRA_TEMPLATE_UID);
		contentView = (LinearLayout) findViewById(R.id.contentLayout);
		reloadContents();

	}

	@SuppressWarnings("unchecked")
	private void reloadContents()
	{
		contentView.removeAllViews();
		SafeEntryTable table = new SafeEntryTable(getApplicationContext());
		Object[] dataList = table.getEntry(templateUID, folderName);
		entryFieldList = (ArrayList<TemplateFieldContainer>) dataList[0];
		String status = getResources().getString(R.string.created_on) + " " + (String) dataList[1];
		setStatusText(status);
		TemplateFieldContainer container;
		boolean hasNoEntries = true;
		LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
		for(int i = 0; i < entryFieldList.size(); i++)
		{
			container = entryFieldList.get(i);
			if(container.getFieldValue() != null && container.getFieldValue().length() > 1)
			{
				addEntryField(container, inflater);
				hasNoEntries = false;
			}
		}
		if(hasNoEntries)
		{
			findViewById(R.id.noEntries).setVisibility(View.VISIBLE);
		}

	}

	@Override
	protected void setupUI(Bundle savedInstance)
	{
		setContentView(R.layout.activity_viewsafeentry);
	}
}
